package com.lzy.article.controller;

import com.lzy.api.controller.article.CommentControllerApi;
import com.lzy.api.controller.user.BaseController;
import com.lzy.article.service.CommentPortalService;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.pojo.AppUser;
import com.lzy.pojo.vo.AppUserVO;
import com.lzy.pojo.bo.CommentReplyBO;
import com.lzy.utils.PagedGridResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;


@RestController
public class CommentController extends BaseController implements CommentControllerApi {

    final static Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CommentPortalService commentPortalService;

    @Override
    public GraceJSONResult createComment(CommentReplyBO commentReplyBO) {

        System.out.println(commentReplyBO.toString());


        // 2. 根据留言用户id查询他的昵称，用于冗余存入留言记录，避免多表管理查询的性能开支
        String userId = commentReplyBO.getCommentUserId();

        // 3. 发起restTemplate调用获得用户基本信息
        Set<String> idSet = new HashSet<>();
        idSet.add(userId);
        AppUserVO appUser = getPublisherList(idSet).get(0);
        String nickname = appUser.getNickname();
        String userFace = appUser.getFace();
        // 3. 保存评论信息
        commentPortalService.createComment(commentReplyBO.getArticleId(),
                commentReplyBO.getFatherId(),
                commentReplyBO.getContent(),
                userId,
                nickname,
                userFace);

        return GraceJSONResult.ok();
    }

    @Override
    public GraceJSONResult commentCounts(String articleId) {

        Integer counts =
                getCountsFromRedis(REDIS_ARTICLE_COMMENT_COUNTS + ":" + articleId);

        return GraceJSONResult.ok(counts);
    }

    @Override
    public GraceJSONResult list(String articleId, Integer page, Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult =
                commentPortalService.queryArticleComments(articleId,
                        page,
                        pageSize);
        return GraceJSONResult.ok(gridResult);
    }

    @Override
    public GraceJSONResult mng(String writerId, Integer page, Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = commentPortalService.queryWriterCommentsMng(writerId, page, pageSize);
        return GraceJSONResult.ok(gridResult);
    }


    @Override
    public GraceJSONResult delete(String writerId, String commentId) {
        commentPortalService.deleteComment(writerId, commentId);
        return GraceJSONResult.ok();
    }

}
